#include "..\headers\ListOf.h"

typedef struct LNodeList {
  int data;
  int index;
  struct LNodeList *next;
} LNodeList, *LinkList;

void freeList(LinkList head) {
  LinkList temp;
  while (head) {
    temp = head;
    head = head->next;
    free(temp);
  }
}

LinkList CreatList() {
  int length;
  int data, index;
  printf("Length:");
  scanf("%d", &length);
  if (length <= 0) return NULL;
  LinkList head = NULL, p1 = NULL, p2 = NULL, Node = NULL;
  int len = length;
  head = (LinkList)malloc(sizeof(LNodeList));
  if (head == NULL) {  // judge whether allocate memory was successful
    freeList(head);
    return NULL;
  }
  head->next = NULL;
  while (len--) {
    printf("please input parameter and index:");
    scanf("%d %d", &data, &index);
    if (p1 != NULL && index == p1->index) {
      p1->data += data;
      continue;
    }
    p1 = (LinkList)malloc(sizeof(LNodeList));
    if (p1 == NULL) {  // judge whether allocate memory was successful
      freeList(p1);
      return NULL;
    }
    p1->data = data;
    p1->index = index;
    if (Node == NULL) {
      Node = p1;
      head->next = Node;
    } else {
      p2->next = p1;
    }
    p2 = p1;
  }
  p2->next = NULL;
  head->data = length;
  return head;
}

int ScanfData(LinkList &L, int data,
              int index) {  // Always pass in as the last node
  if (L == NULL) return ERROR;
  LinkList p1 = NULL;
  if (data != 0) {
    p1 = (LinkList)malloc(sizeof(LNodeList));
    p1->index = index, p1->data = data;
    L->next = p1;
    p1->next = NULL;
  }
  return OK;
}

LinkList AddLists(LinkList &L, LinkList &Q) {
  if (L == NULL && Q != NULL) {
    return Q;
  }
  if (L != NULL && Q == NULL) {
    return L;
  }
  if (L == NULL && Q == NULL) {
    return NULL;
  }
  L = L->next;
  Q = Q->next;
  LinkList Result, p = NULL;
  Result = (LinkList)malloc(sizeof(LNodeList));
  Result->next = NULL;
  p = Result;
  int data, index;
  while (L && Q) {
    if (L->index == Q->index) {
      data = L->data + Q->data;
      index = Q->index;
      L = L->next, Q = Q->next;
    } else if (L->index > Q->index) {
      data = Q->data;
      index = Q->index;
      Q = Q->next;
    } else if (L->index < Q->index) {
      data = L->data;
      index = L->index;
      L = L->next;
    }
    ScanfData(p, data, index);  // Store data in a linked list
    p = p->next;
  }
  if (L == NULL && Q != NULL) p->next = Q;
  if (L != NULL && Q == NULL) p->next = L;
  return Result;
}

LinkList SubtractionLists(LinkList &L, LinkList &Q) {
  if (L == NULL && Q != NULL) {
    LinkList q = Q;
    while (q) {
      q->data = -q->data;
      q = q->next;
    }
    return Q;
  }
  if (L != NULL && Q == NULL) {
    return L;
  }
  if (L == NULL && Q == NULL) {
    return NULL;
  }
  L = L->next;
  Q = Q->next;
  LinkList Result, p = NULL;
  Result = (LinkList)malloc(sizeof(LNodeList));
  Result->next = NULL;
  p = Result;
  int data, index;
  while (L && Q) {
    if (L->index == Q->index) {
      data = L->data - Q->data;
      index = Q->index;
      L = L->next, Q = Q->next;
    } else if (L->index > Q->index) {
      data = Q->data;
      index = Q->index;
      Q = Q->next;
    } else if (L->index < Q->index) {
      data = L->data;
      index = L->index;
      L = L->next;
    }
    ScanfData(p, data, index);  // Store data in a linked list
    p = p->next;
  }
  if (L == NULL && Q != NULL) p->next = Q;
  if (L != NULL && Q == NULL) p->next = L;
  return Result;
}

int main() {
  LinkList L, Q, l;
  L = CreatList();
  Q = CreatList();
  if (L == NULL && Q == NULL) {
    printf("ERROR IN CREATLIST");
    return ERROR;
  }
  int flag;
  printf("please input a number 1 represent add 0 represent subtraction:");
  while (1) {
    scanf("%d", &flag);
    if (flag == 1 || flag == 0)
      break;
    else
      printf("ERROR SCANF");
  }
  LinkList Result;
  if (flag == 1) Result = AddLists(L, Q);
  if (flag == 0) Result = SubtractionLists(L, Q);
  l = Result->next;
  if (l == NULL) printf("0");
  while (l) {
    if (l->data != 1) printf("%d", l->data);
    if (l->index != 0 && l->index != 1) printf("x^%d", l->index);
    if (l->index == 1) printf("x");
    if ((l->next != NULL) && (l->next->data > 0)) printf("+");
    l = l->next;
  }
  freeList(Result);
  return OK;
}
